package com.uenpay.im.ui.old.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.widget.TextView;

import com.aswife.ui.ASWWebView;
import com.qiyunxin.android.http.listener.OnWebChromeClientListener;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;

import java.lang.reflect.InvocationTargetException;

@SuppressLint("SetJavaScriptEnabled")
/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class WebViewActivity extends Activity {
	private ASWWebView webView;
	private TextView titleTextView;
	private int type = 0;

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		IMApplication.getInstance().addActivity(this);
		initView();
		if (savedInstanceState != null) {
			webView.restoreState(savedInstanceState);
		}
		initListener();
		String url = getIntent().getStringExtra("url");
		webView.loadUrl(url);
	}

	public void initView() {

		webView = (ASWWebView) findViewById(R.id.webView);
		titleTextView = (TextView) findViewById(R.id.title_textview);
		if (type == 1) {
			titleTextView.setText(getResources().getString(
					R.string.privacy_policy));
		} else {
			titleTextView.setText(getResources().getString(R.string.about_app));
		}
		webView.OnChromeClientListener(new OnWebChromeClientListener() {

			@Override
			public void OnReceivedTitle(WebView view, String title) {
				titleTextView.setText(title);
			}

			@Override
			public void OnReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
			}

			@Override
			public void OnPageFinished(WebView view, String url) {
			}

			@Override
			public void OnProgressChanged(WebView view, int newProgress) {

			}

			@Override
			public void OnOpenFileChooser(Intent intent) {
			}
		});

		webView.setDownloadListener(new DownloadListener() {

			@Override
			public void onDownloadStart(String url, String userAgent,
					String contentDisposition, String mimetype,
					long contentLength) {
			}
		});
	}

	public void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
	}

	@SuppressLint("NewApi")
	@Override
	protected void onResume() {
		if (webView != null) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				webView.onResume();
			} else {
				try {
					webView.getClass().getMethod("onResume")
							.invoke(webView, (Object[]) null);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				}
			}
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@SuppressLint("NewApi")
	@Override
	protected void onPause() {
		if (webView != null) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				webView.onPause();
			} else {
				try {
					webView.getClass().getMethod("onPause")
							.invoke(webView, (Object[]) null);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				}
			}
		}
		super.onPause();
	}

}
